<?php
namespace frontend\controllers;

use Yii;
use frontend\models\Member;

class ApiController extends \yii\web\Controller
{

	public function actionIndex($value='')
	{
		return 1234;
		# code...
	}

	public function actionLogin($username,$pwd){
		$result = [
			'success'=>false,
			'errorMsg'=>'',
			'result'=>[],
		];
		//查找用户
		$member = Member::findOne(['username'=>$username]);
		//验证密码
		if($member!=null){
			$rs = Yii::$app->security->validatePassword($pwd,$member->password_hash);
			if($rs){
				$result['success'] = true;
				$result['result'] = [
					"id"=>$member->id,
			       "userName"=>$member->username,
			       "userIcon"=>Yii::getAlias("@web").'/images/star.jpg',
			       "waitPayCount"=>1,
			       "waitReceiveCount"=>2,
			       "userLevel"=>2
				];
				return \yii\helpers\Json::encode($result);
			}
		}
		$result['errorMsg'] = "用户名或密码错误";
		

		return \yii\helpers\Json::encode($result);
	}


	public function actionBanner($adKind='')
	{
		$result = [
			"success"=>true,
			"errorMsg"=>"",
			"result"=>[
				'id'=>1,
				'type'=>1,
				'adUrl'=>\Yii::getAlias('@web').'/images/hot1.jpg',
				'adKind'=>$adKind,
			],
		];

		return \yii\helpers\Json::encode($result);
		/**
{
   "success": true,
   "errorMsg": "",
   "result": [
     {
       "id": 广告id,
       "type": 跳转类型（1跳转到网页，2跳转到商品详情，3跳转到分类去）,
       "adUrl": "图片路径",
       "webUrl": "如果是跳转网页类型，则返回网页地址",
       "adKind": 广告类型（1为导航banner，2为广告banner）
      }
   ]
}
		
		*/
	}

	public function actionSeckill($value='')
	{
		$result = [
			"success"=>true,
			"errorMsg"=>"",
			"result"=>[
				'total'=>12,
				'rows'=>[
					'allPrice'=>19.00,
					'pointPrice'=>10.00,
					'iconUrl'=>\Yii::getAlias('@web').'/images/hot1.jpg',
					'type'=>2,
					'productId'=>1,
				]
			],
		];
		return \yii\helpers\Json::encode($result);
		/**
{
  "success": true,
  "errorMsg": "",
  "result": {
      "total": "秒杀商品总数",
      "rows": [
        {
          "allPrice": "原价",
          "pointPrice": "秒杀价格",
          "iconUrl": "商品图片路径",
          "timeLeft": 秒杀剩余时间（分钟）,
          "type": 秒杀类型（1抢年货，2超值，3热卖）,
          "productId": 商品id
        }
      ]
    }
}

		*/
	}
	public function actionGetYourFav($value='')
	{
		$result = [
			"success"=>true,
			"errorMsg"=>"",
			"result"=>[
				'total'=>12,
				'rows'=>[
					'price'=>19.00,
					'name'=>'商品名称',
					'iconUrl'=>\Yii::getAlias('@web').'/images/hot1.jpg',
					'type'=>2,
					'productId'=>1,
				]
			],
		];
		return \yii\helpers\Json::encode($result);

	}


	public function actionRedis(){
		//测试redis在yii2种的使用
		//实例化redis对象
		//$redis = \Yii::$app->redis;
		//添加数据
		//$redis->set('key','name');
		//获取数据
		//$res =$redis->get('key');
		//var_dump($res);
		// $redis = new redis();
		// $result = $redis->connect('127.0.0.1', 6379);  
		// var_dump($result); //结果：bool(true)  
		//$redsi->flushall();//删除redsi中的所有的数据
	}
	public function actionSessionRedis(){

	}


	/** 发送电子邮件测试 */
    public function actionSendMail(){
    	$mail = Yii::$app->mailer;
        $r = $mail->compose()
            ->setFrom('abc@longphp.xin')
            ->setTo('nelsonsun_mellon@hotmail.com')
            ->setSubject('Message subject')
            ->setTextBody('Plain text content')
            ->setHtmlBody('<b>HTML content</b>')
            ->send();

           var_dump($mail);
    }

}



